package xjy.smartcommunity.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import xjy.smartcommunity.R;
import xjy.smartcommunity.adapter.GeneralAdapter;
import xjy.smartcommunity.bean.General;

public class WuYeActivity extends AppCompatActivity {
    //    private ListView listView;
    private List<General> generals;//要显示的数据集合
    private ListView lvGenerals;//ListView对象
//    private BaseAdapter generalAdapt;//适配器
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wu_ye);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);// 给左上角图标的左边加上一个返回的图标
        //设置背景图片的透明度
        View v = findViewById(R.id.activity_wu_ye);
        v.getBackground().setAlpha(40);
        lvGenerals = (ListView) findViewById(R.id.baoXiuInfoLv);
        lvGenerals.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                towuyeDetailActivity(position);
            }
        });
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                init();//初始化要显示的数据集合、ListView对象、以及适配器
            }
        });

        thread.start();
    }
    private void towuyeDetailActivity(int pos) {
//        ListView listView = (ListView) findViewById(R.id.baoXiuInfoLv);
//        HashMap<String, String> map = (HashMap<String, String>) lvGenerals.getItemAtPosition(pos);
//        根据position的值去后台查询响应的物业人员
        String posid = String.valueOf(pos);
        Intent intent = new Intent(this, WuYeRYActivity.class);
        intent.putExtra("id", posid);
        startActivity(intent);
    }
    /**
     *  根据后期需要大量更改
     */
    private void init() {
        //初始化要显示的数据集合---start
        generals = new ArrayList<General>();
        //图片资源集合
        int[] resImags = {
                R.drawable.home_yuliu, R.drawable.home_yuliu, R.drawable.home_yuliu,
                R.drawable.home_yuliu, R.drawable.home_yuliu
        };
        //将资源中的字符串组数转换为Java数组
        String[] names = getResources().getStringArray(R.array.generals);
        for (int i = 0; i < resImags.length; i++) {
            General general = new General(resImags[i], names[i]);
            generals.add(general);
        }
        //初始化要显示的数据集合---end
        //初始化listView
        lvGenerals = (ListView) findViewById(R.id.baoXiuInfoLv);
        //初始化适配器以及设置该listView的适配器
        GeneralAdapter generalAdapt = new GeneralAdapter(generals);
        lvGenerals.setAdapter(generalAdapt);
    }

//    class GeneralAdapter extends BaseAdapter {
//        //得到listView中item的总数
//        @Override
//        public int getCount() {
//            return generals.size();
//        }
//
//        @Override
//        public Object getItem(int position) {
//            return generals.get(position);
//        }
//
//        @Override
//        public long getItemId(int position) {
//            return position;
//        }
//
//        //简单来说就是拿到单行的一个布局，然后根据不同的数值，填充主要的listView的每一个item
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//        //拿到ListViewItem的布局，转换为View类型的对象
//            View layout = View.inflate(WuYeActivity.this, R.layout.listitem_bao_xiu_info, null);
//        //找到显示物业类型的ImageView
//            ImageView ivThumb = (ImageView) layout.findViewById(R.id.imageview);
//        //找到显示物业类型的TextView
//            TextView tvName = (TextView) layout.findViewById(R.id.baoXiuTypeTv);
//        //获取军事中下标是position的物业类型对象
//            General general = generals.get(position);
//        //显示图片
//            ivThumb.setImageResource(general.getImageSrc());
//        //显示内容
//            tvName.setText(general.getTextInfo());
//            return layout;
//        }
    }
//    private void showZhiNan() {
//        String urlStr = getString(R.string.server_host) + "/getAllBaoXiuInfo.do";
//        String errMsg = "";
//        Bundle bundle = new Bundle();
//        try {
//            JSONObject json = HttpUtils.requestJson(this, urlStr, null, false);
//            if (json.getInt("status") != 0)
//                throw new Exception(json.getString("message"));
//            JSONArray arr = json.getJSONArray("data");
//            ArrayList<HashMap<String, String>> list = new ArrayList<>();
//            for (int i = 0; i < arr.length(); i++) {
//                JSONObject jsonObject = arr.getJSONObject(i);
//                HashMap<String, String> map = new HashMap<>();
//                map.put("type", jsonObject.getString("type"));
////                map.put("name", jsonObject.getString("name"));
////                map.put("phone", jsonObject.getString("phone"));
//                map.put("id", jsonObject.getString("id"));
//                list.add(map);
//            }
//            bundle.putSerializable("list", list);
//        } catch (Exception ex) {
//            errMsg = ex.getMessage();
//            if (errMsg == null || errMsg.length() == 0)
//                errMsg = "请求失败";
//        }
//        bundle.putString("errMsg", errMsg);
//        Message msg = new Message();
//        msg.setData(bundle);
//        handler.sendMessage(msg);
//    }
//
//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            String errMsg = msg.getData().getString("errMsg");
//            if (errMsg.length() > 0) {
//                Toast.makeText(getApplicationContext(), errMsg, Toast.LENGTH_SHORT).show();
//                return;
//            }
//            ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.getData().getSerializable("list");
////            SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),list, R.layout.listitem_bao_xiu_info,
////                    new String[]{"type","name","phone","id"},new int[]{R.id.baoXiuTypeTv,R.id.baoXiuNameTv,R.id.baoXiuPhoneTv,R.id.baoXiuIdTv});
//            SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list, R.layout.listitem_bao_xiu_info,
//                    new String[]{"type"}, new int[]{R.id.baoXiuTypeTv});
//            ListView listView = (ListView) findViewById(R.id.baoXiuInfoLv);
//            listView.setAdapter(adapter);
//        }
//    };
//
//}


